/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hvtt.web.controller;

import com.hvtt.web.connection.Command;
import com.hvtt.web.connection.WEBConnection;
import com.hvtt.web.connection.WEBParamCollection;
import com.hvtt.web.models.Sys_ngonngu;
import com.hvtt.web.models.Sys_ngonngudet;
import com.hvtt.web.utilities.Utilities;
import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.context.RequestContext;

/**
 *
 * @author Administrator
 */
@ManagedBean
@SessionScoped
public class Sys_NgonNguDetController implements Serializable{
     private WEBConnection DAL=new WEBConnection();
    private Sys_ngonngudet item =new Sys_ngonngudet();
    
    public Sys_ngonngudet getItem()
    {
        return item;
    }
    public void setItem(Sys_ngonngudet item)
    {
        this.item=item;
    }
    
    
    public List<Sys_ngonngudet> findAllNgonNguDet() throws SQLException
    {
        WEBParamCollection Params=new WEBParamCollection();
        Params.add(this.item.getNgonNguID());
        //DAL.executeQuery(null, Command.sqlText, Params, null)
        List<Sys_ngonngudet> lst= DAL.executeQuery("Select* from sys_ngonngudet where NgonNguID=?",Command.sqlText,Params,Sys_ngonngudet.class);
        return lst;        
    }
    public List<Sys_ngonngu> findAllNgonNgu() throws SQLException
    {
        List<Sys_ngonngu> lst=new ArrayList<>();
        if(!FacesContext.getCurrentInstance().isPostback())
        {
            lst= DAL.executeQuery("Select* from Sys_NgonNgu", Sys_ngonngu.class);
            if("".equals(this.item.getNgonNguID().trim()) && lst.size()>0)
                this.item.setNgonNguID(lst.get(0).getNgonNguID());
                  
        }
        return lst;
    }
    public List<Sys_ngonngudet> findAllNgonNguDet(String NgonNguID) throws SQLException
    {
        WEBParamCollection Params=new WEBParamCollection();
        Params.add(NgonNguID);
        List<Sys_ngonngudet> lst= DAL.executeQuery("Select* from sys_ngonngudet where NgonNguID=?",Command.sqlText,Params,Sys_ngonngudet.class);
        return lst;        
    }
     private String validateFields() throws SQLException {
        String str="";
        if (StringUtils.isBlank(this.getItem().getDinhNghia()))
        {
            str= "  + " + Utilities.getLanguages("DinhNghia")+": " + Utilities.getLanguages("Require") + "<br/>";
        }
        if (StringUtils.isBlank(this.getItem().getNoiDung()))
        {
            str+="  + " + Utilities.getLanguages("NoiDung")+": " + Utilities.getLanguages("Require") + "<br/>";
        }
       
        if("".equals(str.trim()))
            return "";
        return "<b style='color:black;'>" + Utilities.getLanguages("AdMsg0010") + ":</b> <br/>" +  "<b style='color:red;'>" + str + "</b>";
    }
    
    
    
    public void btnSave_Click(ActionEvent event) throws IOException, SQLException {
       
        
          this.getItem().setDinhNghia(String.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("DinhNghia")));
          this.getItem().setNoiDung(String.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("NoiDung")));
          this.getItem().setNgonNguID(String.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("NgonNguID")));
          
           String result = this.validateFields();
      
            if (!result.isEmpty()) {
                FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Message Status",result);
                FacesContext.getCurrentInstance().addMessage(null, msg);
                RequestContext.getCurrentInstance().execute("update=\"growl\"");
                return;
          }
          try   
          {
            WEBParamCollection Params=new WEBParamCollection();
            Params.add("Save");
            Params.add(this.item.getDinhNghia());
            Params.add(this.item.getNoiDung());
            Params.add(this.item.getNgonNguID());
            //Params.add(this.item.getBieuTuong());
            DAL.executeUpdate("{call Proc_sys_ngonngudet_In_Up_Del(?,?,?,?)}", Command.sqlStoredProcedure, Params);
            //FacesContext.getCurrentInstance().getExternalContext().redirect("/Admin/sys/languagedif.html");
            FacesMessage msg1 = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Message Status",Utilities.getLanguages("SaveSuccess"));
            FacesContext.getCurrentInstance().addMessage(null, msg1);
            RequestContext.getCurrentInstance().execute("update=\"growl\"");
            
          }
          catch(Exception ex)
          {
              throw  ex;
          }
    }
    
     public void btnDelete_Click(ActionEvent event) throws IOException, SQLException {
       
        
           String DinhNghia=String.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("DinhNghia"));
          String NgonNguID=String.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("NgonNguID"));
         
          try   
          {
            WEBParamCollection Params=new WEBParamCollection();
            Params.add("Delete");
            Params.add(DinhNghia);
            Params.add("");
            Params.add(NgonNguID);
            DAL.executeUpdate("{call Proc_sys_ngonngudet_In_Up_Del(?,?,?,?)}", Command.sqlStoredProcedure, Params);
            FacesContext.getCurrentInstance().getExternalContext().redirect("/Admin/sys/languagedif.html");
          }
          catch(Exception ex)
          {
              throw  ex;
          }
    }
    public void btnUpdate_Click(ActionEvent event) throws IOException, SQLException {
       
        
          String DinhNghia=String.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("DinhNghia"));
          String NgonNguID=String.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("NgonNguID"));
         
          try   
          {
            WEBParamCollection Params=new WEBParamCollection();
            Params.add("GetByKey");
            Params.add(DinhNghia);
            Params.add("");
            Params.add(NgonNguID);
            List<Sys_ngonngudet> lst= DAL.executeQuery("{call Proc_sys_ngonngudet_In_Up_Del(?,?,?,?)}", Command.sqlStoredProcedure, Params,Sys_ngonngudet.class);
            if(lst!=null && lst.size()>0)
            {
                this.setItem(lst.get(0));
                FacesContext.getCurrentInstance().getExternalContext().redirect("/Admin/sys/languagedif/new.html");
            }
          }
          catch(Exception ex)
          {
              throw  ex;
          }
    }
    
    public void btnNgonNgu_Change(ActionEvent event) throws IOException, SQLException {
          this.item.setNgonNguID(String.valueOf(
                FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("NgonNguID")));
         
         
    }
    
    
}
